package 字符串;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author Weton Li
 * @Date 2021/8/25 20:51
 */
public class 查找常用字符串 {
    public static void main(String[] args) {
        String[] s = {"bella","label","roller"};

        for (String str : commonChars(s)) {
            System.out.println(str+"\t");
        }
    }

    public static List<String> commonChars(String[] words) {
        int[] arrMin = new int[26];
        Arrays.fill(arrMin, Integer.MAX_VALUE);

        for (String word : words) {
            int[] arr = new int[26];
            int n = word.length();
            for (int i = 0; i < n; i++) {
                char c = word.charAt(i);
                arr[c - 'a']++;
            }
            for (int i = 0; i < 26; i++) {
                arrMin[i] = Math.min(arrMin[i],arr[i]); // 0代表a，25代表z
            }
        }

        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < arrMin[i]; j++) {
                list.add(String.valueOf((char)(i+'a')));
            }
        }
        return list;
    }
}
